package com.xx.pmis.web.actions;

import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.Diary;
import com.xx.pmis.services.DiaryService;
import com.xx.pmis.web.forms.DiaryForm;
import com.xx.pmis.web.util.CurrentDate;
import com.xx.pmis.web.util.SplitAndFilterString;

/**
 * @class 日记管理Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class DiaryAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(DiaryAction.class);

	DiaryService diaryService = getPMISServices().getDiaryService();

	// 查询所有日记
	public ActionForward listDiary(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<?> all = null;
		try {
			all = diaryService.getAllDiary();
		} catch (Exception e) {
			String errorPage = "【文档管理/日记】查询【日记】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		Iterator<?> it = all.iterator();
		while (it.hasNext()) {
			Diary diary = (Diary) it.next();
			diary.setDiaryText(SplitAndFilterString.splitAndFilterString(diary.getDiaryText(), 10000000));
		}
		request.setAttribute("diaryList", all);
		request.setAttribute("position", "文档管理/日记");
		return mapping.findForward("listDiary");
	}

	// 登录日志模块权限认证
	public ActionForward loginDiary(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("position", "日记/权限认证");
		return mapping.findForward("loginDiary");
	}

	// 登录日志模块权限认证,处理
	public ActionForward loginDiary_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		DiaryForm f = (DiaryForm) form;
		String loginPassword = f.getLoginPassword();
		if (!loginPassword.equals("1061220414")) {
			String errorPage = "【文档管理/日记/权限认证】您输入的登录密码不正确，请核实后再输入！";
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		this.listDiaryByPage(mapping, form, request, response);
		return mapping.findForward("listDiary");
	}

	// 新增日记
	public ActionForward addDiary(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("diaryDate", CurrentDate.currentDateIncludeE());
		return mapping.findForward("addDiary");
	}

	// 新增，处理方法
	public ActionForward addDiary_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		DiaryForm f = (DiaryForm) form;
		String diaryDate = f.getDiaryDate(); // 日期
		String weather = f.getWeather(); // 天气
		String diaryText = f.getDiaryText(); // 正文

		Diary diary = new Diary();
		diary.setDiaryDate(diaryDate);
		diary.setWeather(weather);
		diary.setDiaryText(diaryText);
		diary.setEditTime(CurrentDate.currentDateAndTime());
		diary.setEditNum("1");
		try {
			diaryService.saveOrUpdateDiary(diary);
		} catch (Exception e) {
			String errorPage = "【文档管理/日记/新增】保存或更新【日记】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		listDiaryByPage(mapping, form, request, response);
		return mapping.findForward("listDiary");
	}

	// 编辑
	public ActionForward editDiary(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String diaryID = request.getParameter("diaryID");
		Diary diary = null;
		try {
			diary = diaryService.getDiaryByDiaryID(diaryID);
		} catch (Exception e) {
			String errorPage = "【文档管理/日记/编辑】根据【日记ID】查询该【日记】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("diary", diary);
		return mapping.findForward("editDiary");
	}

	// 编辑，处理方法
	public ActionForward editDiary_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		DiaryForm f = (DiaryForm) form;
		String diaryID = f.getDiaryID();
		String weather = f.getWeather(); // 天气
		String diaryText = f.getDiaryText(); // 正文

		Diary diary = null;
		try {
			diary = diaryService.getDiaryByDiaryID(diaryID);
		} catch (Exception e) {
			String errorPage = "【文档管理/日记/编辑】根据【日记ID】查询该【日记】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		diary.setWeather(weather);
		diary.setDiaryText(diaryText);
		diary.setEditTime(CurrentDate.currentDateAndTime());
		diary.setEditNum(String.valueOf(Integer.parseInt(diary.getEditNum()) + 1));

		try {
			diaryService.saveOrUpdateDiary(diary);
		} catch (Exception e) {
			String errorPage = "【文档管理/日记/编辑】保存或更新【日记】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		listDiaryByPage(mapping, form, request, response);
		return mapping.findForward("listDiary");
	}

	// 删除
	public ActionForward delDiary(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Diary diary = null;
		DiaryForm f = (DiaryForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				diary = diaryService.getDiaryByDiaryID(ids[i]);
			} catch (Exception e) {
				String errorPage = "【文档管理/日记/删除】根据【日记ID】查询该【日记】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
			try {
				diaryService.deleteDiary(diary);
			} catch (Exception e) {
				String errorPage = "【文档管理/日记/删除】保存或更新【日记】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
		}

		listDiaryByPage(mapping, form, request, response);
		return mapping.findForward("listDiary");
	}

	// 明细
	public ActionForward detailDiary(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String diaryID = request.getParameter("diaryID");

		Diary diary = null;
		try {
			diary = diaryService.getDiaryByDiaryID(diaryID);
		} catch (Exception e) {
			String errorPage = "【文档管理/日记/明细】根据【日记ID】查询该【日记】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("diary", diary);
		return mapping.findForward("detailDiary");
	}

	// 分页显示
	public ActionForward listDiaryByPage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		int PAGESIZE = 20;// 每页记录数
		int totalPage = 1;// 总页数

		// 查询总用户记录数
		int count = 0;
		try {
			count = diaryService.getCountOfDiary();
		} catch (Exception e) {
			String errorPage = "【文档管理/日记】查询【日记】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("count", count);
		if (count % PAGESIZE == 0) {
			totalPage = count / PAGESIZE;
		} else {
			totalPage = count / PAGESIZE + 1;
		}
		request.setAttribute("totalPage", totalPage);
		// 声明要翻的页
		String pageNum = "1";
		try {
			String thisPageNum = request.getParameter("pageNum");
			// 如果传了页码参数
			if (thisPageNum != null && thisPageNum.length() != 0) {
				pageNum = thisPageNum;
			}
		} catch (RuntimeException e) {
			log.info("" + e.toString());
		}
		request.setAttribute("pageNum", pageNum);
		List<?> all = null;
		try {
			all = diaryService.getAllDiaryByPage(Integer.parseInt(pageNum), PAGESIZE);
		} catch (Exception e) {
			String errorPage = "【文档管理/日记】查询【日记】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("diaryList", all);
		request.setAttribute("PAGESIZE", PAGESIZE);
		// 如果要翻的这页是第一页
		if ("1".equals(pageNum)) {
			// 设置noUp为yes(界面上用此控制'首页'与'上一页'按钮失效)
			request.setAttribute("noUp", "yes");
		}

		// 如果要翻的这页是最后页
		if (totalPage == Integer.parseInt(pageNum)) {
			// 设置noDown为yes(界面上用此控制'下一页'与'尾页'按钮失效)
			request.setAttribute("noDown", "yes");
		}
		
		if(totalPage == 0) {
			request.setAttribute("pageNum", "0");
			request.setAttribute("noUp", "yes");
			request.setAttribute("noDown", "yes");
		}

		request.setAttribute("action", "diary.do");
		request.setAttribute("position", "文档管理/日记");
		return mapping.findForward("listDiary");
	}
	
	// 新增，处理方法
	public ActionForward addDiary_Auto(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String diaryText = request.getParameter("diaryText");
		System.out.println("********************************************************");
		System.out.println("********************************************" + diaryText);

		Diary diary = new Diary();
		diary.setDiaryText(diaryText);
		diary.setEditTime(CurrentDate.currentDateAndTime());
		diary.setEditNum("1");
		try {
			diaryService.saveOrUpdateDiary(diary);
		} catch (Exception e) {
			String errorPage = "【文档管理/日记/新增】保存或更新【日记】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		listDiaryByPage(mapping, form, request, response);
		return mapping.findForward("listDiary");
	}

}